package coolingPoolDevice

import (
	"gorm.io/gorm"
	"xkginweb/global"
	"xkginweb/model/entity/coolingPoolDevice"
	"xkginweb/model/entity/coolingPoolDevice/request"
)

// 定义coolingPoolDevice的service提供coolingPoolDevice的数据curd的操作

type CoolingPoolDeviceService struct{}

// @author: hcl
// @function: CreateCoolingPoolDevice
// @description: 创建冷池检擦项
// @param: coolingPoolDevice.QccCoolingPoolDevice
// @return: err error

func (service *CoolingPoolDeviceService) CreateCoolingPoolDevice(coolingPoolDevice *coolingPoolDevice.QccCoolingPoolDevice) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	err = global.KSD_DB.Create(coolingPoolDevice).Error
	return err
}

//@author: hcl
//@function: UpdateCoolingPoolDevice
//@description: 更新冷池检擦项
//@param: coolingPoolDevice.QccCoolingPoolDevice
//@return: err error

func (service *CoolingPoolDeviceService) UpdateCoolingPoolDevice(coolingPoolDevice *coolingPoolDevice.QccCoolingPoolDevice) (err error) {
	//err = global.KSD_DB.Save(coolingPoolDevice).Error
	err = global.KSD_DB.Model(coolingPoolDevice).Updates(coolingPoolDevice).Error
	return err
}

// @author: hcl
// @function: DeleteCoolingPoolDevice
// @description: 删除冷池检擦项
// @param: coolingPoolDevice.QccCoolingPoolDevice
// @return: err error

func (service *CoolingPoolDeviceService) DeleteCoolingPoolDevice(coolingPoolDevice *coolingPoolDevice.QccCoolingPoolDevice) (err error) {
	err = global.KSD_DB.Delete(&coolingPoolDevice).Error
	return err
}

// @author: hcl
// @function: DeleteCoolingPoolDeviceById
// @description: 根据ID删除冷池检擦项
// @param: uint
// @return: err error

func (service *CoolingPoolDeviceService) DeleteCoolingPoolDeviceById(id uint) (err error) {
	var coolingPoolDevice coolingPoolDevice.QccCoolingPoolDevice
	err = global.KSD_DB.Where("cooling_pool_device_id = ?", id).Delete(&coolingPoolDevice).Error
	return err
}

// @author: hcl
// @function: GetCoolingPoolDevice
// @description: 根据ID获取冷池检擦项信息
// @param: id uint
// @return: coolingPoolDevice *coolingPoolDevice.QccCoolingPoolDevice, err error

func (service *CoolingPoolDeviceService) GetCoolingPoolDevice(id uint) (coolingPoolDevice *coolingPoolDevice.QccCoolingPoolDevice, err error) {
	err = global.KSD_DB.Where("cooling_pool_device_id = ?", id).Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
		return db.Table("qcc_room").Select("room_id,room_name,room_type")
	}).Preload("QccCoolingPool", func(db *gorm.DB) *gorm.DB {
		return db.Table("qcc_cooling_pool").Select("cooling_pool_id,cooling_pool_name")
	}).First(&coolingPoolDevice).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadCoolingPoolDevicePage
//@description: 分页获取冷池检擦项列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *CoolingPoolDeviceService) LoadCoolingPoolDevicePage(info request.CoolingPoolDevicePageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&coolingPoolDevice.QccCoolingPoolDevice{})

	// 准备切片帖子数组
	var CoolingPoolDeviceList []coolingPoolDevice.QccCoolingPoolDevice

	//if info.Status != -1 {
	//	db = db.Where("status = ?", info.Status)
	//}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(device_name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return CoolingPoolDeviceList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
			return db.Table("qcc_room").Select("room_id,room_name,room_type")
		}).Preload("QccCoolingPool", func(db *gorm.DB) *gorm.DB {
			return db.Table("qcc_cooling_pool").Select("cooling_pool_id,cooling_pool_name")
		}).Find(&CoolingPoolDeviceList).Error
	}

	// 结果返回
	return CoolingPoolDeviceList, total, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadCoolingPoolDevice
//@description: 获取冷池检擦项列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *CoolingPoolDeviceService) LoadCoolingPoolDevice(info request.CoolingPoolDeviceInfo) (list []*coolingPoolDevice.QccCoolingPoolDeviceList, err error) {

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&coolingPoolDevice.QccCoolingPoolDevice{})

	// 准备切片帖子数组
	list = make([]*coolingPoolDevice.QccCoolingPoolDeviceList, 1, 1)

	if info.RoomId != nil {
		db = db.Where("room_id = ?", info.RoomId)
	}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(coolingPool_device_name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 执行查询
	err = db.Table("qcc_cooling_pool_device").Omit("created_at", "updated_at", "is_deleted", "room_id", "room_name", "room_type").Find(&list).Error

	// 结果返回
	return list, err
}
